//
//  WHFishSpotSearchVC.m
//  WHEasyBizLink
//
//  Created by ShengMeng on 9/20/25.
//

#import "WHFishSpotSearchVC.h"
#import "WHFishSpotTableViewCell.h"
#import "WHEnterpriseCompanyModel.h"
#import "WHFishSpotModel.h"
#import "SearchTitleView.h"
#import "SDAutoLayout.h"
#import "WHTdsModel.h"
#import "MJRefresh.h"

#define pagesize 80

static NSString *const kCellID = @"WHFishSpotSearchVCCellID";

@interface WHFishSpotSearchVC ()
@property(nonatomic,assign)CGFloat historyY;
@property (nonatomic,assign)int  mPageIndex;
@property(nonatomic,strong)NSString*searchString;

@end

@implementation WHFishSpotSearchVC
- (void)viewDidLoad {
    self.mPageIndex = 1;
    self.searchString = @"";
    [super viewDidLoad];
    //self.view.backgroundColor =[UIColor whiteColor];
    self.title = @"搜索";
  
}

#pragma mark - loadDatas
- (void)loadDatas{
    [super loadDatas];
    
    //[self getComListWithisdropDown:NO];
    
    self.datas =   [WHFishSpotModel getDatasModel:10];
    
}

#pragma mark - setupUI
- (void)setupUI{
    [super setupUI];
    //一个简单的super setupUI = createTableView
    self.tableView.frame =CGRectMake(0,navHeight1+40,ScreenW,ScreenH-navHeight1-40);
    //self.navigationItem.rightBarButtonItem =self.rightItem;
    
    self.view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"enterprise_bg"].CGImage);
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.subviews.firstObject.alpha = 0;
    
    self.searchView.backgroundColor =[UIColor clearColor];
    self.searchView.searchBar.backgroundColor =[UIColor whiteColor];
    self.searchView.searchBar.placeholder     = @"搜索钓场/类型/鱼种";
    self.tableView.backgroundColor =[UIColor clearColor];

}

-(void)rightBarButtonItemClicked
{
    
}

#pragma mark 接收服务平台改变通知
-(void)notificationAction:(NSNotification *)notification{
    [self getComListWithisdropDown:NO];
}

#pragma mark - 查询公司列表
- (void)getComListWithisdropDown:(BOOL)isdropDown{
    NSDictionary *tds = [WHTdsModel getSelTDSProxyList];
    NSArray *allkey = [tds allKeys];
    if (allkey.count==0) {
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    dic[@"currPage"]     = [NSString stringWithFormat:@"%d",self.mPageIndex];//self.comModel.id;
    dic[@"perPageCount"] = [NSString stringWithFormat:@"%d",pagesize];
    dic[@"udid"]         = kApp.userPhone;
    dic[@"conditions"]   = self.searchString;
    dic[@"proxy"]        = tds[@"url"];
    dic[@"target"]       = tds[@"TDSid"];

    [WHEnterpriseCompanyModel  qryListCompanyWithFromdata:dic succesefulBlock:^(id  _Nonnull responseObject) {
        NSArray *dataList = (NSArray*)responseObject;
        
        if (dataList.count < pagesize){
            //拿到当前的小于Request_count时，表示没有更多的数据了
            [self setViewFooterWithHave:NO];
        }else{
            [self setViewFooterWithHave:YES];
            // 拿到当前的不小于Request_count时，表还有更多的数据了
            [self setViewFooterEndRefresh];
        }
        
        NSMutableArray *mutabelArray;
        if(isdropDown){
            [self.datas addObjectsFromArray:dataList];
            mutabelArray = self.datas;
        }else{
            mutabelArray = [NSMutableArray arrayWithArray:dataList];
        }
        self.datas =[NSMutableArray arrayWithArray:mutabelArray];
    } andFailureBlock:^(NSString * _Nonnull error) {
        
        self.datas = [NSMutableArray new];
    }];
}

#pragma mark 下拉刷新开始
- (void)dropDownRefresh
{
    if(!self.tableView.mj_footer) return;
    self.mPageIndex++;
    [self getComListWithisdropDown:YES];
}

- (void)setViewFooterWithHave:(BOOL) have{
    if(have){
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self dropDownRefresh];
        }];
    }else{
        self.tableView.mj_footer =nil;
    }
}

- (void)setViewFooterEndRefresh{
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - <searchViewDelegate>
-(void)searchTileViewTextFieldDidChange:(NSString*)textFieldString
{
    if (textFieldString.length==0)
    {
        self.mPageIndex = 1;
        self.searchString     = @"";
        [self.datas removeAllObjects];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        [self getComListWithisdropDown:NO];
    }
}

//点击搜索调用
-(void)searchTileViewSearchButtonClickSearchString:(NSString *)searchString
{
    self.searchString = searchString;
    self.mPageIndex = 1;
    [self.datas removeAllObjects];
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshing];
    [self getComListWithisdropDown:NO];
}

#pragma mark - <tableViewDelegate>
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   

    WHFishSpotTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:kCellID];
    if (!cell) {
        cell = [[WHFishSpotTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
    }
    cell.model = self.datas[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView
 heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 300;
    //return kMarginH*16;
}

-(void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}


@end
